package com.zhuyi.broadcast.controller;

import com.zhuyi.broadcast.pojo.domain.Program;
import com.zhuyi.broadcast.service.ProgramService;
import com.zhuyi.common.annotation.Log;
import com.zhuyi.common.core.controller.BaseController;
import com.zhuyi.common.core.domain.AjaxResult;
import com.zhuyi.common.core.page.TableDataInfo;
import com.zhuyi.common.enums.BusinessType;
import com.zhuyi.common.utils.DateUtils;
import com.zhuyi.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 *  节目Controller
 * </p>
 *
 * @author dyj
 * @since 2024-12-12
 */
@Slf4j
@RestController
@RequestMapping("/broadcast/program")
public class ProgramController extends BaseController {

    @Autowired
    private ProgramService programService;

    /**
     * 查询节目（列表）
     */
    @GetMapping("/list")
    public TableDataInfo list(Program program) {
        startPage();//开启分页
        List<Program> list = programService.list(program);
        return getDataTable(list);// 返回分页数据
    }

    /**
     * 查询节目（详细信息）
     */
    @GetMapping("/{programId}")
    public AjaxResult getInfo(@PathVariable Long programId) {
        Program program = programService.getById(programId);
        return AjaxResult.success(program);
    }

    /**
     * 新增节目
     */
    @Log(title = "节目", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public AjaxResult add(@RequestBody Program program) {
        program.setCreateTime(DateUtils.getNowDate());
        program.setUpdateTime(DateUtils.getNowDate());
        program.setCreatedBy(SecurityUtils.getLoginUser().getUsername());
        program.setUpdatedBy(SecurityUtils.getLoginUser().getUsername());
        return toAjax(programService.save(program));
    }

    /**
     * 更新节目
     */
    @Log(title = "节目", businessType = BusinessType.UPDATE)
    @PutMapping("/{programId}")
    public AjaxResult updateProgram(@PathVariable Long programId,
                                    @RequestBody Program program) {
        try {
            program.setId(programId);
            program.setUpdatedBy(SecurityUtils.getLoginUser().getUser().getNickName());
            program.setUpdateTime(DateUtils.getNowDate());
            programService.updateById(program);
            return success(program);
        } catch (Exception e) {
            log.error("[ProgramController::updateProgram]更新节目出现错误，错误消息: {}", e.getMessage());
            e.fillInStackTrace();
        }
        return error("更新节目出现错误");
    }

    /**
     * 删除节目
     */
    @Log(title = "节目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{programId}")
    public AjaxResult remove(@PathVariable Long programId) {
        return toAjax(programService.removeProgram(programId));
    }
    /**
     * 发送节目
     * @param programId 节目 ID
     * @param deviceId  设备 ID
     * @return AjaxResult 发送操作结果
     */
    @PostMapping("/send")
    public AjaxResult sendProgram(@RequestParam Long programId, @RequestParam Long deviceId) {
        boolean result = programService.sendProgram(programId, deviceId);
        if (result) {
            return success("发送节目成功");
        } else {
            return error("发送节目失败");
        }
    }


}
